package com.geeklan.smart.umeng;

import android.app.Notification;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import com.geeklan.base.utils.MLog;
import com.geeklan.smart.MLMApplication;
import com.geeklan.smart.data.model.UmengMessage;
import com.geeklan.smart.utils.SharedPreferencesUtil;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.api.UPushAliasCallback;
import com.umeng.message.api.UPushRegisterCallback;
import com.umeng.message.api.UPushTagCallback;
import com.umeng.message.common.inter.ITagManager;
import com.umeng.message.entity.UMessage;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

public class UmengManager implements UPushRegisterCallback {
    private static final String TAG = "Umang";
    private static final String appkey = "677f49fa92421c0b4c8fcecd";
    private static final String Secret = "f4b390382b981c6da4d641c46d0a7206";
    private static final String channel = "Umeng";
    private MLMApplication application;
    private PushAgent api;
    private String[] tags;

    public UmengManager(MLMApplication context) {
        this.application = context;
        //友盟日志输出开关
        UMConfigure.setLogEnabled(false);
        //预初始化
        UMConfigure.preInit(context, appkey, channel);
    }

    public void init() {
        //建议在子线程中初始化
        new Thread(() -> {
            UMConfigure.init(application, appkey, channel, UMConfigure.DEVICE_TYPE_PHONE, Secret);
            // 注册推送
            api = PushAgent.getInstance(application);
//            api.setNotificationOnForeground(true);//应用在前台，设置不显示通知消息
            api.register(this);
            api.setMessageHandler(msgHandler);
            api.setNotificationClickHandler(notificationClickHandler);
            api.setDisplayNotificationNumber(20);
//            api.setMessageHandler(messageHandler);
//            api.setPushIntentServiceClass(MyCustomMessageService.class);
            api.onAppStart();
        }).start();
    }

    public void addTags(String... tag) {
        tags = tag;
        api.getTagManager().addTags(new UPushTagCallback<ITagManager.Result>() {
            @Override
            public void onMessage(boolean b, ITagManager.Result result) {

            }
        }, tag);
    }

    public void deleteTags(String tag) {
        api.getTagManager().deleteTags(new UPushTagCallback<ITagManager.Result>() {
            @Override
            public void onMessage(boolean b, ITagManager.Result result) {

            }
        }, tag);
    }

    public void deleteTags() {
        api.getTagManager().deleteTags(new UPushTagCallback<ITagManager.Result>() {
            @Override
            public void onMessage(boolean b, ITagManager.Result result) {

            }
        }, tags);
    }

    public void addAlias(String userId) {
        api.addAlias(userId, "userid", new UPushAliasCallback() {
            @Override
            public void onMessage(boolean b, String s) {

            }
        });
    }

    public void deleteAlias(String userId) {
        api.deleteAlias(userId, "userid", new UPushAliasCallback() {
            @Override
            public void onMessage(boolean isSuccess, String message) {

            }
        });
    }

    @Override
    public void onSuccess(String deviceToken) {
        MLog.i(TAG, "deviceToken: " + deviceToken);
//        copyToClipboard(application,deviceToken);
    }
    private void copyToClipboard(Context context, String text) {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", text);
        clipboard.setPrimaryClip(clip);
//        Toast.makeText(context, "已复制到剪切板", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(String errCode, String errDesc) {
        MLog.e(TAG, "register failed! " + "code:" + errCode + ",desc:" + errDesc);
    }

    //处理通知栏消息
    UmengMessageHandler msgHandler = new UmengMessageHandler() {
        //处理通知栏消息
        @Override
        public void dealWithNotificationMessage(Context context, UMessage msg) {
            sendEventMessage(msg);
        }

        //自定义通知样式，此方法可以修改通知样式等
        @Override
        public Notification getNotification(Context context, UMessage msg) {
            MLog.e(TAG, "getNotification" + msg.getTitle());
            return super.getNotification(context, msg);
        }

        //处理透传消息
        @Override
        public void dealWithCustomMessage(Context context, UMessage msg) {
            MLog.e(TAG, "dealWithCustomMessage" + msg.getTitle());
        }
    };

    public void sendEventMessage(UMessage msg){
        Map<String, String> map = msg.getExtra();
        String ask_time = map.get("ask_time");
        String device_time_task_id = map.get("device_time_task_id");
        String msgType = map.get("msgType");
        String title = msg.getTitle();
        String content = msg.getContent();
        MLog.e(TAG, "UmengMessageHandler" + "\nask_time:" + ask_time + "\ndevice_time_task_id:" + device_time_task_id
                + "\nmsgType:" + msgType + "\ntitle:" + title + "\ncontent:" + content);
        boolean isLogin = SharedPreferencesUtil.getBoolean("isLogin", false);
        UmengMessage umengMessage = new UmengMessage();
        umengMessage.setAsk_time(ask_time);
        umengMessage.setDevice_time_task_id(device_time_task_id);
        umengMessage.setMsgType(msgType);
        umengMessage.setTitle(title);
        umengMessage.setContent(content);
        if (isLogin) {
//            判断msgtype == 1 是消息类型 弹框后确认跳转到消息页面
//            判断msgtype == 2 是定时计划询问类型  判断执行时间ask_time超过5分钟 就不弹框 默认超时不执行 没超过5分钟就弹框，调用接口
            EventBus.getDefault().post(umengMessage);
        }
    }

    //点击通知的打开动作
    UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
        @Override
        public void dealWithCustomAction(Context context, UMessage msg) {
            MLog.e(TAG, "dealWithCustomAction" + msg.getTitle());
        }

        @Override
        public void openActivity(Context context, UMessage msg) {
            MLog.e(TAG, "openActivity" + msg.getTitle());
        }

        @Override
        public void launchApp(Context context, UMessage msg) {
            MLog.e(TAG, "launchApp" + msg.getTitle());
            sendEventMessage(msg);
        }

        @Override
        public void dismissNotification(Context context, UMessage msg) {
            MLog.e(TAG, "dismissNotification" + msg.getTitle());
        }
    };
}
